package com.yjy.lab03.controller;

import com.yjy.lab03.model.RegionStats;
import com.yjy.lab03.service.RegionStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class RegionStatsController {

    @Autowired
    public RegionStatsService service;

    @GetMapping("/main")
    public String showRegionStats(Model model){    //显示主页面
        List<RegionStats> regionStatsList = service.getAllRegionStats();
        String lastTime = service.getLastTime();
        int sum = service.getSum();
        model.addAttribute("sum",sum);
        model.addAttribute("lastTime",lastTime);
        model.addAttribute("regionStatsList",regionStatsList);
        return "main";
    }

    @PostMapping("/search")
    public String showSearchRegionStats(Model model,@ModelAttribute("str") String str){  //显示搜索后的页面
        int sum = service.getSum();
        String lastTime = service.getLastTime();
        List<RegionStats> regionStatsList = service.search(str);
        model.addAttribute("sum",sum);
        model.addAttribute("lastTime",lastTime);
        model.addAttribute("regionStatsList",regionStatsList);
        return "main";
    }

}
